<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>CMSIS-RTOS RTX: Tick Timer Configuration</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="cmsis.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="stylsheetf" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 46px;">
  <td id="projectlogo"><img alt="Logo" src="CMSIS_Logo_Final.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">CMSIS-RTOS RTX
   &#160;<span id="projectnumber">Version 4.74</span>
   </div>
   <div id="projectbrief">CMSIS-RTOS RTX: Real-Time Operating System for Cortex-M processor-based devices</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<div id="CMSISnav" class="tabs1">
    <ul class="tablist">
      <li class="current"><a href="./index.html"><span>RTX</span></a></li>
    </ul>
</div>
<!-- Generated by Doxygen 1.8.2 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Usage&#160;and&#160;Description</span></a></li>
      <li><a href="modules.html"><span>Reference</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('_timer_tick.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Tick Timer Configuration </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The CMSIS-RTOS RTX functions provide delays in units of milliseconds that are derived from RTX Timer Tick. It is therefore recommended to configure the RTX Timer tick to generate a 1 millisecond interval. Configuring a longer RTX Timer Tick may reduce energy consumption, but has impacts on the granularity of the timeouts.</p>
<ul>
<li><a class="el" href="_timer_tick.html#SysTick">Usage of the Cortex-M SysTick timer as RTX kernel timer</a></li>
<li><a class="el" href="_timer_tick.html#AltTimer">Usage of an alternative timer as RTX kernel timer.</a></li>
<li><a class="el" href="_timer_tick.html#LowPower">Configuration for Low-Power modes</a></li>
</ul>
<p>The value <b>#define OS_CLOCK</b> specifies the Cortex-M processor clock frequency in Hz. This value is used to calculate the RTX kernel timer reload value.</p>
<p>The value <b>#define OS_TICK</b> specifies the RTX Timer Tick interval in microseconds (us). This value is used to calculate timeout values. When the SysTick core timer is enabled the value is also used to configure the SysTick timer. It is recommended to configure the RTX Timer tick to 1000 us which results in a timeout granularity of 1 millisecond.</p>
<h1><a class="anchor" id="SysTick"></a>
Usage of the Cortex-M SysTick timer as RTX kernel timer</h1>
<p>With <b>#define OS_SYSTICK 1</b> the Cortex-M SysTick timer is selected as RTX kernel timer. In this case, the RTX kernel configures the SysTick timer clock source as processor clock. Therefore the value <b>OS_CLOCK</b> should be identical with the value of the CMSIS variable <b>SystemCoreClock</b>.</p>
<h1><a class="anchor" id="AltTimer"></a>
Usage of an alternative timer as RTX kernel timer.</h1>
<p>With <b>#define OS_SYSTICK 0</b> an alternative timer is selected as RTX kernel timer.</p>
<p>Three functions in the <a class="el" href="_r_t_x__conf__c_m.html">RTX_Conf_CM.c Configuration File</a> need to be adapted for using an alternative hardware timer.</p>
<ul>
<li><b>int os_tick_init (void)</b> provides the initialization function for the alternative hardware timer.</li>
<li><b>void os_tick_irqack (void)</b> is an interrupt acknowledge function that is called to confirm the alternative hardware timer interrupt.</li>
<li><b>OS_Tick_Handler</b> needs to be called as the hardware timer interrupt function; the startup code should be modified to this function.</li>
</ul>
<p><b>Configuration Code:</b> </p>
<div class="fragment"><div class="line"><span class="comment">/*--------------------------- os_tick_init ----------------------------------*/</span></div>
<div class="line"><span class="keywordtype">int</span> os_tick_init (<span class="keywordtype">void</span>) {</div>
<div class="line">  <span class="comment">/* Initialize alternative hardware timer as RTX kernel timer. */</span></div>
<div class="line">  <span class="comment">/* ... */</span></div>
<div class="line">  <span class="keywordflow">return</span> (-1);  <span class="comment">/* Return IRQ number of timer (0..239) */</span></div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">/*--------------------------- os_tick_irqack --------------------------------*/</span></div>
<div class="line"><span class="keywordtype">void</span> os_tick_irqack (<span class="keywordtype">void</span>) {</div>
<div class="line">  <span class="comment">/* Acknowledge timer interrupt. */</span></div>
<div class="line">  <span class="comment">/* ... */</span></div>
<div class="line">}</div>
</div><!-- fragment --><h1><a class="anchor" id="LowPower"></a>
Configuration for Low-Power modes</h1>
<p>The system thread <b>os_idle_demo</b> can be use to switch the system into a low-power mode. The easiest form to enter a low-power mode is the execution of the __WFE function that puts the processor into a sleep mode where it waits for a event.</p>
<p><b>Configuration Example:</b></p>
<pre class="fragment">#include "device.h"                     /* Device definitions                 */

void os_idle_demon (void) {
  /* The idle demon is a system thread, running when no other thread is       */
  /* ready to run.                                                            */

  for (;;) {
    __WFE();                            /* Enter sleep mode                   */
  }
}
</pre><dl class="section note"><dt>Note</dt><dd>__WFE() is not available at every Cortex-M implementation. Check device manuals for availability.</dd></dl>
<h1><a class="anchor" id="TickLess"></a>
Tick-less operation</h1>
<p>CMSIS-RTOS RTX provides extension for tick-less operation which is useful for applications that use extensively low-power modes where the SysTick timer is also disabled. To provide a time-tick in such power-saving modes a wake-up timer is used to derive timer intervals. The RTX functions <b>os_suspend</b> and <b>os_resume</b> control the tick-less operation.</p>
<p>Using this functions allows the RTX thread scheduler to stop the periodic kernel tick interrupt. When all active threads are suspended, the system enters power-down and calculates how long it can stay in this power-down mode. In the power-down mode the processor and potentially peripherals can be switched off. Only a wake-up timer must remain powered, because this timer is responsible to wake-up the system after the power-down period expires.</p>
<p>The tick-less operation is controlled from the <b>os_idle_demon</b> thread. The wake-up timeout value is set before the system enters the power-down mode. The function <b>os_suspend</b> calculates the wake-up timeout measured in RTX Timer Ticks; this value is used to setup the wake-up timer that runs during the power-down mode of the system.</p>
<p>Once the system resumes operation (either by a wake-up time out or other interrupts) the RTX thread scheduler is started with the function <b>os_resume</b>. The parameter <em>sleep_time</em> specifies the time (in RTX Timer Ticks) that the system was in power-down mode.</p>
<p><b>Configuration Example:</b></p>
<pre class="fragment">#include "LPC11Uxx.h"                   /* LPC11Uxx definitions               */

void os_idle_demon (void) {
  /* The idle demon is a system thread, running when no other thread is       */
  /* ready to run.                                                            */
  unsigned int sleep;
  unsigned int tc;

  LPC_SYSCON-&gt;SYSAHBCLKCTRL |=  (1UL &lt;&lt; 15) |  /* Enable clock for WWDT       */
                                (1UL &lt;&lt; 19);   /* Enable clock for Pin IRQ    */

  LPC_SYSCON-&gt;PINTSEL[0]     =   1;            /* P0.1 selected as INT0 IRQ   */
  LPC_SYSCON-&gt;STARTERP0     |=  (1UL &lt;&lt;  0);   /* Enable INT0 wake-up         */
  LPC_SYSCON-&gt;STARTERP1     |=  (1UL &lt;&lt; 12);   /* Enable WWDT wake-up         */

  LPC_SYSCON-&gt;WDTOSCCTRL     =  (3   &lt;&lt;  0) |  /* WDOSC DIVSEL=3              */
                                (2   &lt;&lt;  5);   /* WDOSC FREQ=0.8MHz           */
  LPC_SYSCON-&gt;PDRUNCFG      &amp;= ~(1UL &lt;&lt;  6);   /* Power-up WDT Oscillator     */
  LPC_SYSCON-&gt;PDSLEEPCFG    &amp;= ~(1UL &lt;&lt;  6);   /* Power WDT Oscillator in PD  */

  LPC_WWDT-&gt;CLKSEL           =  (1UL &lt;&lt;  0) |  /* Select WDOSC as Clock       */
                                (1UL &lt;&lt; 31);   /* Lock selection              */
  LPC_WWDT-&gt;WARNINT          =   1000;         /* WDT Warning IRQ value       */
  LPC_WWDT-&gt;MOD              =  (1UL &lt;&lt;  0);   /* Enable WDT                  */

  NVIC_EnableIRQ(FLEX_INT0_IRQn);              /* Enable INT0 IRQ (wake-up)   */
  NVIC_EnableIRQ(WDT_IRQn);                    /* Enable WWDT IRQ (wake-up)   */

  for (;;) {
  /* HERE: include optional user code to be executed when no task runs.*/
    sleep = os_suspend();                      /* Suspend RTX thread scheduler */

    if (sleep) {                               /* How long can we sleep?       */
      /* "sleep" is in RTX Timer Ticks which is 10ms in this configuration     */
      
      /* Setup WDT wake-up: WDT ticks @25kHz (FREQ/2/(DIVSEL+1)/4) */
      tc = (sleep * 250) + 1000;
      LPC_WWDT-&gt;TC   = tc;
      LPC_WWDT-&gt;FEED = 0xAA;
      LPC_WWDT-&gt;FEED = 0x55;

      /* Enter Power-down mode */
      LPC_SYSCON-&gt;PDAWAKECFG = LPC_SYSCON-&gt;PDRUNCFG;  /* Power after wake-up   */
      LPC_PMU-&gt;PCON = 0x02;                    /* Select Power-down mode       */
      SCB-&gt;SCR = (1UL &lt;&lt; 2);                   /* Set SLEEPDEEP                */
      __WFE();                                 /* Enter Power-down mode        */
      
      /* After Wake-up */
      sleep = (tc - LPC_WWDT-&gt;TV) / 250;
    }

    os_resume(sleep);                          /* Resume RTX thread scheduler  */
  }
}
</pre><dl class="section note"><dt>Note</dt><dd>__WFE() is not available at every Cortex-M implementation. Check device manuals for availability. </dd></dl>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="_configure.html">Configuration of CMSIS-RTOS RTX</a></li>
    <li class="footer">Generated on Mon Feb 24 2014 11:07:07 for CMSIS-RTOS RTX by ARM Ltd. All rights reserved.
	<!--
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.2 
	-->
	</li>
  </ul>
</div>
</body>
</html>
